package com.heima.admin.controller.v1;

import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.admin.dto.SensitiveDto;
import com.heima.model.admin.pojo.AdSensitive;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 *
 * @author Ronaldo
 * @date 2021/7/30
 */
@RestController
@RequestMapping("/api/v1/sensitive")
@Api(value = "敏感词管理",tags = "sensitive",description = "敏感词管理api")
@Slf4j
public class AdSensitiveController {

    @Autowired
    private AdSensitiveService adSensitiveService;

    @ApiOperation("敏感词列表分页查询")
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody SensitiveDto sensitiveDto){
        log.info("query sensitive list by dto {}" + sensitiveDto);
        if (Objects.isNull(sensitiveDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return adSensitiveService.queryPage(sensitiveDto);
    }

    @ApiOperation("新增敏感词信息")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdSensitive adSensitive){
        log.info("insert sensitive by {}" + adSensitive);
        if (Objects.isNull(adSensitive)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return adSensitiveService.insertSensitive(adSensitive);
    }

    @ApiOperation("更新敏感词信息")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdSensitive adSensitive){
        log.info("update sensitive by {}" + adSensitive);
        if (Objects.isNull(adSensitive)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return adSensitiveService.updateSensitive(adSensitive);
    }

    @ApiOperation("根据id删除敏感词")
    @DeleteMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        log.info("delete sensitive by id {}" + id);
        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return adSensitiveService.deleteById(id);
    }
}